package com.qiaofang.util;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @classname: Export.java
 * @author: Beau
 * @create: 2019/12/18 18:51
 * @version: 1.0.0
 **/
public class Export {
    private static Logger logger= LoggerFactory.getLogger(Export.class);
    public static void exportDevPremisesInfo(List<List<Map<String, String>>> list, List<String[]> headerList, String[] name, String fileName, HttpServletResponse response) throws Exception {
        String mimetype = "application/x-msdownload";
        String inlineType = "attachment";
        response.setHeader("Content-Disposition", inlineType  + ";filename=" + new String(fileName.getBytes(),"iso-8859-1") + ".xls");
        OutputStream out = response.getOutputStream();
        response.setContentType(mimetype);
        try {
            @SuppressWarnings("resource")
            HSSFWorkbook wb = new HSSFWorkbook();
            for(int n = 0; n < name.length; n++){
                HSSFSheet sheet = wb.createSheet();
                wb.setSheetName(n, name[n]);
                HSSFCellStyle headStyle = wb.createCellStyle();
                HSSFFont f  = wb.createFont();
                f.setFontHeightInPoints((short) 12);// 字号
                f.setBold(true);// 加粗
                headStyle.setFont(f);
                headStyle.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
                headStyle.setBorderBottom(BorderStyle.valueOf((short) 1));
                headStyle.setBorderRight(BorderStyle.valueOf((short) 1));
                headStyle.setFillBackgroundColor((short) 20);
                HSSFRow row = sheet.createRow(0);
                //创建单元格，并设置值表头 设置表头居中
                HSSFCellStyle style = wb.createCellStyle();
                style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
                style.setBorderBottom(BorderStyle.valueOf((short) 1));
                style.setBorderRight(BorderStyle.valueOf((short) 1));

                HSSFCell cell = null;
                String[] header = headerList.get(n);
                for (int i = 0; i < header.length; i++) {
                    cell = row.createCell(i);
                    cell.setCellStyle(style);
                    HSSFRichTextString text = new HSSFRichTextString(header[i]);
                    cell.setCellValue(text);
                }

                List<Map<String, String>> mapList = list.get(n);
                for (int j = 0; j < mapList.size(); j++) {
                    Map<String, String> map = mapList.get(j);
                    row = sheet.createRow((int) j + 1);
                    for (int i = 0; i < header.length; i++) {
                        cell = row.createCell(i, CellType.forInt(1));
                        cell.setCellValue(StringUtils.isBlank(map.get(header[i]) + "_" + j) ? "" : map.get(header[i] + "_" + j));
                    }
                }
            }

            wb.write(out);

        } catch (Exception e) {
            logger.error("exportChannelEffectExcel error: {}", e);
        }finally {
            out.flush();
            out.close();
        }
    }
}
